#include<stdlib.h>
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/wait.h>
#define STY_LEFT "["
#define STY_RIGHT "]"
#define COMMAND_SIZE 1024
#define NUM 20
#define PWD_SIZE 1024
#define SEP " ' ' '\t'"
using namespace std;
int lastcode;
char argv[COMMAND_SIZE];
char* com[NUM];
char pwd[PWD_SIZE];
char* getuser()
{
    return getenv("USER");
}

char* gethostname()
{
    return getenv("HOSTNAME");
}

char* getpwd()
{
    getcwd(pwd,sizeof(pwd));
}

void commandline()
{
    getpwd();
    cout << STY_LEFT <<getuser() << '@' << gethostname() << ' ' << pwd <<STY_RIGHT << '$';
}

void getcommand(char* argv, int size)
{
    fgets(argv, size, stdin);
    argv[strlen(argv)-1] = '\0';
}

void splitecommand(char* argv, char** com)
{
    int i = 0;
    com[i++] = strtok(argv, SEP);
    while(com[i++] = strtok(NULL, SEP));
}
void Normalexecute(char** _com)
{    
    pid_t id = fork();
        if(id  <= 0)
        {
            perror("fail");
            return;
        }
        else if(id == 0)
        {
            execvp(_com[0], _com);
            exit(404);
        }
        else{
            int status = 0;
            pid_t rid = waitpid(id,&status,0);
            if(rid == id){
                lastcode = WEXITSTATUS(status);
            }

        }
}

int main()
{
    while(1)
    {
         commandline();
         getcommand(argv,strlen(argv)-1);
         splitecommand(argv, com);
     //    Normalexecute(com);
    } 
    return 0;
}
